﻿@model XStudio.Service.Timing.Entity.TimingFlowEntity
@using XStudio.Core.Ext
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script type="text/javascript">

    $(function () {
        $.validator.setDefaults({
            errorClass: "tip-error",
            errorPlacement: function (error, element) {
                error.prependTo(element.parent().next())
            }
        });
        var v = $('#frmInput').validate({
            rules: {
                Flow: { required: true, number: true, min: 1 },
                FlowName: { required: true },
                Assembly: { required: true },
                ClassName: { required: true },
                OkFLow: { required: true, number: true, min: 0 },
                ErrFlow: { required: true, number: true, min: 0 },
                TryTimes: { required: true, number: true, min: 1 },
                TryInterval: { required: true, number: true, min: 1 },
                Timeout: { required: true, number: true, min: 1 }
            },
            messages: {
                Flow: { required: "必填项！", number: "必须为数字！", min: "请输入大于1的数字" },
                FlowName: { required: "必填项！" },
                Assembly: { required: "必填项！" },
                ClassName: { required: "必填项！" },
                OkFLow: { required: "必填项！", number: "必须为数字！", min: "请输入大于0的数字" },
                ErrFlow: { required: "必填项！", number: "必须为数字！", min: "请输入大于0的数字" },
                Timeout: { required: "必填项！", number: "必须为数字！", min: "请输入大于1的数字" },
                TryTimes: { required: "必填项！", number: "必须为数字！", min: "请输入大于1的数字" },
                TryInterval: { required: "必填项！", number: "必须为数字！", min: "请输入大于1的数字" }
            }
        });

        //保存按钮点击事件
        $("#btnSave").click(function () {
            if (!$("#frmInput").valid())
                return false;

            SaveParam();
            $.ajax({
                url: $('#frmInput').attr("action"),
                data: $('#frmInput').serialize(),
                type: "Post",
                error: function (request) {
                    alert('保存定时服务流程出错，请重试！');
                },
                success: function (data) {
                    if (data.IsOK) {
                        $.closeWin($("#btnSave"));

                        //更新列表
                        $(".tabs-panels .panel:visible").find('#tbFlowList').datagrid('reload');
                    }
                    else
                        alert(data.Description);
                }
            });
        });

        var params = new Array();
        var paramItems = $("#txtParam").val().split('∮');
        if ($("#txtParam").val() != "") {
            for (var i = 0; i < paramItems.length; i++) {
                params[i] = paramItems[i].split('∞')
            }
        }
        Params = params;
        RenderParam();
    });

    var Params = new Array();
    function AddParam() {
        //验证参数
        var paramItems = Params;
        var key = $("#txtParamKey").val();
        if (key == "") {
            alert("请输入参数键名！");
            $("#txtParamKey").focus();
            return false;
        }
        for (var i = 0; i < paramItems.length; i++) {
            if (paramItems[i][0] == key) {
                alert("已有此参数！");
                $("#txtParamKey").select();
                $("#txtParamKey").focus();
                return false;
            }
        }

        var item = new Array(2);
        item[0] = key;
        item[1] = $("#txtParamValue").val();
        Params[Params.length] = item;
        RenderParam();
    }

    function RenderParam() {
        var paramItems = Params;
        var htmlBuilder = new Array();
        htmlBuilder[0] = "<table class='tbParam' ><tr class='ListHead'><td width='100px'>参数名</td><td>参数值</td><td width='50px'></td></tr>";
        if (paramItems != null && paramItems.length > 0) {
            for (var i = 0; i < paramItems.length; i++) {
                htmlBuilder[i + 1] = "<tr><td>" + paramItems[i][0] + "</td><td>" + paramItems[i][1] + "</td><td style='text-align:center;'><input type='button' value='删' onclick='RemoveParam(" + i + ");' /></td></tr>";
            }
        }
        htmlBuilder[paramItems.length + 1] = "</table>";
        $("#paramValue").html(htmlBuilder.join(""));
    }

    function RemoveParam(removeIndex) {
        if (confirm("确认要删除参数吗？")) {
            var paramItems = Params;
            var tempParam = new Array();
            var newIndex = 0;
            for (var i = 0; i < paramItems.length; i++) {
                if (removeIndex != i) {
                    tempParam[newIndex] = paramItems[i];
                    newIndex++;
                }
            }
            Params = tempParam;
            RenderParam();
        }
    }


    //保存参数
    function SaveParam() {
        var paramItems = Params;
        var paramBuilder = new Array();
        //paramBuilder[paramBuilder.length] = "<root>"
        for (var i = 0; i < paramItems.length; i++) {
            //paramBuilder[paramBuilder.length] = "<item key=\"" + paramItems[i][0] + "\" value=\"" + paramItems[i][1] + "\" />";
            paramBuilder[paramBuilder.length] = paramItems[i][0] + "∞" + paramItems[i][1] + "∮";
        }
        //paramBuilder[paramBuilder.length] = "</root>"

        $("#txtParam").val(paramBuilder.join(""));
    }

</script>
<form name="frmInput" id="frmInput" method=post action="@Url.Action(@ViewContext.RouteData.Values["Action"].ToString(), new { flowId = Model.UID })" >
<table class="tbl-input" cellspacing="0">
    <colgroup>
        <col style="width: 100px;" />
        <col />
        <col style="width: 90px;" />
        <col class="w2" />
    </colgroup>
    <tbody>
        <tr>
            <th>
                步骤序号：
            </th>
            <td>
                @Html.TextBoxFor(t => Model.Flow, new { style = "width:20px;", maxlength = "2" })
                名称：
                @Html.TextBoxFor(t => Model.FlowName, new { style = "width:181px;", maxlength = "50" })
            </td>
            <td class="slight">
                <span class="btn-infomust">必填项</span>
            </td>
        </tr>
        <tr>
            <th>
                程序集：
            </th>
            <td>
                @Html.TextBoxFor(t => Model.Assembly, new { style = "width:250px;", maxlength = "50" })
            </td>
            <td class="slight">
                <span class="btn-infomust">必填项</span>
            </td>
        </tr>
        <tr>
            <th>
                类&nbsp;名&nbsp;称：
            </th>
            <td>
                @Html.TextBoxFor(t => Model.ClassName, new { style = "width:250px;", maxlength = "100" })
            </td>
            <td class="slight">
                <span class="btn-infomust">必填项</span>
            </td>
        </tr>
        <tr>
            <th>
                下一步骤：
            </th>
            <td>
                成功：
                @Html.TextBoxFor(t => Model.OkFLow, new { style = "width:20px;", maxlength = "2" })
                失败：
                @Html.TextBoxFor(t => Model.ErrFlow, new { style = "width:20px;", maxlength = "2" })
            </td>
            <td class="slight">
                <span class="btn-infomust">必填项</span>
            </td>
        </tr>
        <tr>
            <th>
                失败重试：
            </th>
            <td>
                次数：
                @Html.TextBoxFor(t => Model.TryTimes, new { style = "width:20px;", maxlength = "2" })
                间隔：
                @Html.TextBoxFor(t => Model.TryInterval, new { style = "width:20px;", maxlength = "4" })
                (秒)
            </td>
            <td class="slight">
                <span class="btn-infomust">必填项</span>
            </td>
        </tr>
        <tr>
            <th>
                超时时间：
            </th>
            <td>
                @Html.TextBox("Timeout", Model.ExtData.GetRegexNode("Timeout"), new { style = "width:100px;", maxlength = "10" })
                (分钟)
            </td>
            <td class="slight">
                <span class="btn-infomust">必填项</span>
            </td>
        </tr>
        <tr>
            <th>
                参数：
            </th>
            <td colspan="2">
                名称：<input id="txtParamKey" style="width: 70px" />
                值：<input id="txtParamValue" width="100px" />
                <input type="button" value="添加" onclick="AddParam();" />
                <input type="hidden" name="Param" value="@Model.ParamData" id="txtParam" />
            </td>
        </tr>
        <tr>
            <th>
            </th>
            <td colspan="2">
                <div id="paramValue" style="height: 140px; overflow: auto; border: 1px solid #333">
                </div>
            </td>
        </tr>
        <tr>
            <th>
                备注：
            </th>
            <td>
                @Html.TextAreaFor(t => t.Remark, new { rows = 3, style = "width:90%;" })
            </td>
            <td class="slight">
                <span class="btn-info">选填项</span>
            </td>
        </tr>
        <tr>
            <td colspan="4" style="text-align: center;">
                <p class="op">
                    <input type="button" id="btnSave" class="btn-8" value="保存" />
                    <input type="button" value="返回" onclick="$.closeWin(this);" class="btn-8" />
                </p>
            </td>
        </tr>
    </tbody>
</table>
</form>
